﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop.admin
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Order detail";
            if (!Page.IsPostBack)
            {
                DropDownList1.DataSource = dc.Languages.ToList();
                DropDownList1.DataTextField = "name";
                DropDownList1.DataValueField = "id";
                DropDownList1.DataBind();
                if (Request.QueryString["id"] == null)
                    Response.Redirect("CustomerOrdered.aspx");
                else
                {
                    loaddata(1);
                }
            }
        }
        private void loaddata(int langID)
        {
            var dataproduct = from pr in dc.Products
                              join prl in dc.ProductsLanguages
                              on pr.id equals prl.productsId
                              join orderD in dc.OrderDetails on
                               pr.id equals orderD.productsId
                              select new
                              {
                                  OrderID = orderD.orderId,
                                  OrderName = orderD.Order.name,
                                  ProductID = pr.id,
                                  ProductName = prl.name,
                                  ProductPrice = pr.price,
                                  ProductDiscount = pr.discount,
                                  productLanguage = prl.languageId,
                                  sales = orderD.price,
                                  Quantity = orderD.quantity
                              };
            var datalist = dataproduct.Where(od => od.OrderID == Convert.ToInt32(Request.QueryString["id"]) && od.productLanguage == langID).ToList();

            if (datalist.Count > 0)
            {
                ListView1.DataSource = datalist;
                ListView1.DataBind();
                lblordername.InnerHtml = datalist[0].OrderName.ToString();
            }
            else
                lblordername.InnerText = " is empty";
        }
      
        public decimal ProductTotal()
        {
            decimal total = 0;
            foreach (ListViewDataItem item in ListView1.Items)
            {
                int orderid = Convert.ToInt32((item.FindControl("hdfOrderID") as HiddenField).Value);
                int productid = Convert.ToInt32((item.FindControl("hdfProductID") as HiddenField).Value);
                OrderDetail ord = dc.OrderDetails.Single(or => or.orderId == orderid && or.productsId == productid);
                total += Convert.ToDecimal(ord.quantity * ord.price);
            }
             
            
            return total;
        }
     
        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            int orderid = Convert.ToInt32((e.Item.FindControl("hdfOrderID") as HiddenField).Value);
            int productid = Convert.ToInt32((e.Item.FindControl("hdfProductID") as HiddenField).Value);
            OrderDetail ord = dc.OrderDetails.Single(or => or.orderId == orderid && or.productsId == productid);
            decimal total = Convert.ToDecimal(ord.quantity * ord.price);
            ((Label)e.Item.FindControl("lblTotal")).Text = total.ToString();
            
        }
        protected void lbtnCustomerOrdered_Click(object sender, EventArgs e)
        {
            if (Session["user"] != null)
                Response.Redirect("CustomerOrdered.aspx?id=" + Session["user"]);
            else
                Response.Redirect("CustomerOrdered.aspx");
        }

        protected void DropDownList1_selectchanged(object sender, EventArgs e)
        {
            int language = Convert.ToInt32(DropDownList1.SelectedValue);
            loaddata(language);
        }

        protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            DataPager1.SetPageProperties(e.StartRowIndex, 10, false);
              int language = Convert.ToInt32(DropDownList1.SelectedValue);
              loaddata(language);
        }


    }
}